﻿
using RpcModel;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class ErrorCollect : BasicCollect<ErrorCollectDAL>, IErrorCollect
        {
                public ErrorDatum[] Query (string code, IBasicPage paging, out long count)
                {
                        return this.BasicDAL.Query (code, paging, out count);
                }
                public long SetErrorMsg (long errorId, string lang, string msg)
                {
                        ErrorDatum error = this.GetError (errorId);
                        IErrorLangMsgCollect msgCollect = this.GetCollect<IErrorLangMsgCollect> ();
                        using (ITransaction tran = new LocalTransaction ())
                        {
                                msgCollect.SyncError (errorId, lang, msg);
                                this.BasicDAL.SetIsPerfect (errorId);
                                tran.Commit ();
                        }
                        RpcClient.RpcClient.Error.ResetError (errorId, error.ErrorCode);
                        return errorId;
                }

                public long SyncError (ErrorAddDatum add)
                {
                        long errorId = this._SyncError (add.ErrorCode);
                        IErrorLangMsgCollect lang = this.GetCollect<IErrorLangMsgCollect> ();
                        using (ITransaction tran = new LocalTransaction ())
                        {
                                lang.SyncError (errorId, add.Lang, add.Msg);
                                this.BasicDAL.SetIsPerfect (errorId);
                                tran.Commit ();
                        }
                        RpcClient.RpcClient.Error.ResetError (errorId, add.ErrorCode);
                        return errorId;
                }


                public ErrorDatum GetError (long id)
                {
                        return this.BasicDAL.Get (id);
                }
                private long _AddError (string code)
                {
                        return this.BasicDAL.Add (code);
                }
                private long _SyncError (string code)
                {
                        long errorId = this.BasicDAL.FindId (code);
                        if (errorId != 0)
                        {
                                return errorId;
                        }
                        return this._AddError (code);
                }

                public ErrorDatum[] Gets (long[] ids)
                {
                        return base.BasicDAL.Gets (ids);
                }
        }
}
